ETC5513: Collaborative and Reproducible Practices

Tutorial 6

Author

Michael Lydeamore

Published

21 May 2024

🎯 Objectives

  • Create repos in GitHub and clone them into your local computer
  • Practice staging, commit and push workflow
  • Work in a GitHub project as a collaborator
  • Create branches in local and remote repositories
  • Merging branches and deal with conflicts
  • Practice pull requests.

Exercise 1: Working on reproducible reports as a collaborator

  1. Create a new private GitHub repository called Tutorial6-XX and replace XX with your initials. Include a README.md file.

  1. Invite one of your classmates to collaborate in your project. You can do that from GitHub

  1. Accept the invitation and clone your classmate’s repo in your local machine.

git clone https://github.com/ETC5513demo/Tutorial6-XX.git

  1. Continue working in your classmate repo and create a new branch called newbranchXX (replace XX with your initials).
  • git branch newbranchXXX OR git checkout -b newbranchXX if you want to check it out.
  1. In the new branch add a file called Tutorial6XX.qmd (replace XX with your initials). This file should render into html and should have title “Tutorial 6. Collaborating with others” and your name as author.
git checkout newbranchXX
touch Tutorial6XX.qmd

or create the file from RStudio directly.

  1. Add global options for figures sizes and location.
    Hint: knitr::opts_chunk
knitr::opts_chunk$set(echo = TRUE,  fig.width = 8, fig.height = 8, fig.align = "center")
  1. Stage, commit and push changes into your classmate repo
  • git add . or git add Tutorial6.qmd
  • git commit -m "Adding Tutorial6XX.qmd"
  • git push origin newbranchXX

or using RStudio:

  1. Create a new section called “Libraries” and add an R code chunk to load the tidyverse and gridExtra libraries.

Add a section with # Libraries and add the R code chunk:

library(tidyverse)
library(gridExtra)
  1. Stage, commit and push data into your classmate repo
  • git add . or git add Tutorial6XX.qmd
  • git commit -m "Adding libraries and first section to tutorial"
  • git push origin newbranchXX

or use RStudio as above.

  1. Create another section with title “Generating data from a normal distribution with mean 0 and variance 1” and add the following code:
# Simulating data according to a normal distribution

x <- seq(1:100)
y <- rnorm(100)  # Generating data form a normal distribution with mean 0 and variance  1

data <- data.frame(x = x, y = y )    # Creating a data frame

p1 <- ggplot(data, aes(x = x, y = y)) +
   geom_line()

p2 <- ggplot(data, aes(x = x, y = y)) +
   geom_point()

grid.arrange(p1, p2)

# Generating data from a normal distribution with mean 0 and variance 1

  1. Stage, commit and push data into your classmate repo
  • git add . or git add Tutorial6XX.qmd
  • git commit -m "Adding simulation with normal distribution"
  • git push origin newbranchXX

or use RStudio as above.

  1. Merge your branch into the main.
  • git checkout main
  • git merge newbranchXX -m "Merging branches"
  • git push origin main
  1. Stage, commit and push data into your classmate repo
  • git add .
  • git commit -m "Merging branch XX with main branch"
  • git push origin main

or use RStudio as above.

  1. In your GitHub you should have both your repo and your classmate repo. Go to GitHub and have a look at the repos you have and the commits. You should have both your repo and your partner’s repo. In the last commit in both repos you should have the same content by the end of the exercise.

Collaborating with others using pull request (work in pairs for this exercise)

  1. Create a new public GitHub repository called Tutorial6fork-XX and replace XX with your initials. Include a README.md file.

  2. Clone this repo into your local machine.

  3. Create an R project from an existing directory using the directory/folder that you just cloned from GitHub. After you have created the project check that you have a new file in the folder with extension .Rproj. You can also use that file to open a new instance of Rstudio.

  1. Add Tutorial6XX.Rmd from the exercise above into this project. You can simply copy it from the other folder/directory into the new folder/directory that you have created for this new project.

You can either move the file from your other project into this one or use the command cp Tutorial6/Exercise1/Tutorial6XX.qmd Tutorial6/Exercise2/Tutorial6XX.qmd

  1. Stage changes, commit and push changes to the remote repository.
  • git add . or git add Tutorial6XX.qmd
  • git commit -m "Initialising repo with Tutorial6XX.qmd"
  • git push origin main

or use RStudio as above.

  1. Exchange your GitHub repo details with your classmate and fork each others project.

  1. Fork each others repository in your GitHub and clone a local copy.

Navigate to your folder in your computer using cdnad ls and then

git clone https://github.com/PartnerGitHUB_ID/Tutorial6fork-XX.git
  1. Create a new branch and work on this branch.
  • git branch newbranchXX OR
  • git checkout -b newbranchXX which will checkout your newly created branch
  1. Add a new section and a figure into the report.

You can add new sections using # and a new figure using either ![]() or knitr::include_graphics

  1. Stage, commit and push the changes into your remote repo in GitHub.
  • git add . or git add Tutorial6XX.qmd
  • git commit -m "Merging my branch XX with main branch"
  • git push origin main

or use RStudio as above.

  1. Once you have finished with all the changes merge your new branch into main.
  • git checkout main
  • git merge newbranchXX -m "Merging branches"
  • git commit -m "Merging my branch XX with main branch"
  • git push origin main
  1. Create a pull request explaining the changes.

  1. Revise the pull request and accept the proposed changes.

  1. Solve any possible conflict and merge the changes into your original repo that you created in exercise 1.

  1. If you have time repeat steps 8-14 adding more changes in your pull request.